package com.ikingtech.platform.service.system.label.controller;

import com.ikingtech.framework.sdk.context.exception.FrameworkException;
import com.ikingtech.framework.sdk.core.response.R;
import com.ikingtech.framework.sdk.label.api.LabelApi;
import com.ikingtech.framework.sdk.label.embedded.LabelRelationApi;
import com.ikingtech.framework.sdk.label.model.LabelAssignDTO;
import com.ikingtech.framework.sdk.label.model.LabelQueryParamDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.framework.sdk.web.annotation.ApiController;
import com.ikingtech.platform.service.system.label.entity.LabelAssignDO;
import com.ikingtech.platform.service.system.label.entity.LabelDO;
import com.ikingtech.platform.service.system.label.exception.LabelExceptionInfo;
import com.ikingtech.framework.sdk.label.model.LabelDTO;
import com.ikingtech.platform.service.system.label.service.LabelAssignService;
import com.ikingtech.platform.service.system.label.service.LabelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author tie yan
 */
@Slf4j
@RequiredArgsConstructor
@ApiController(value = "/system/label", name = "系统管理-标签管理", description = "系统管理-标签管理，")
public class LabelController implements LabelApi {

    private final LabelService service;

    private final LabelAssignService assignService;

    private final List<LabelRelationApi> relationApis;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<String> add(LabelDTO label) {
        if (Boolean.TRUE.equals(this.service.nameExist(label.getName()))) {
            throw new FrameworkException(LabelExceptionInfo.DUPLICATE_LABEL_NAME);
        }
        LabelDO entity = Tools.Bean.copy(label, LabelDO.class);
        entity.setId(Tools.Id.uuid());
        this.service.save(entity);
        return R.ok(entity.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Object> delete(String id) {
        this.service.removeById(id);
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Object> update(LabelDTO label) {
        if (!this.service.exist(label.getId())) {
            throw new FrameworkException(LabelExceptionInfo.LABEL_NOT_FOUND);
        }
        if (this.service.nameExist(label.getId(), label.getName())) {
            throw new FrameworkException(LabelExceptionInfo.DUPLICATE_LABEL_NAME);
        }
        this.service.updateById(Tools.Bean.copy(label, LabelDO.class));
        return R.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R<Object> assign(LabelAssignDTO labelAssign) {
        this.assignService.removeByBusinessId(labelAssign.getBusinessId());
        if (Tools.Coll.isNotBlank(labelAssign.getLabelIds())) {
            this.assignService.saveBatch(Tools.Coll.convertList(labelAssign.getLabelIds(), labelId -> {
                LabelAssignDO entity = new LabelAssignDO();
                entity.setId(Tools.Id.uuid());
                entity.setBusinessId(labelAssign.getBusinessId());
                entity.setLabelId(labelId);
                return entity;
            }));
        }
        return R.ok();
    }

    @Override
    public R<List<LabelDTO>> page(LabelQueryParamDTO queryParam) {
        return R.ok(this.service.listPage(queryParam).convert(entity -> Tools.Bean.copy(entity, LabelDTO.class)));
    }

    @Override
    public R<List<LabelDTO>> all() {
        return null;
    }

    @Override
    public R<LabelDTO> detail(String id) {
        return null;
    }
}
